package com.imooc.miaosha.controller.BackController;

import com.alibaba.fastjson.TypeReference;
import com.imooc.miaosha.common.Const;
import com.imooc.miaosha.dto.OrderDTO;
import com.imooc.miaosha.dto.UserDTO;
import com.imooc.miaosha.service.UserService;
import com.imooc.miaosha.util.CookieUtil;
import com.imooc.miaosha.util.JedisUtil;
import com.imooc.miaosha.util.JsonData;
import com.imooc.miaosha.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/manage/user")
public class UserManageController {
    @Autowired
    UserService userService;
@RequestMapping("/login")
    @ResponseBody
    public JsonData login(String telephone,String password){
UserDTO userDTO=userService.login(password,telephone,null);
if (userDTO==null){
    return JsonData.fail("用户登陆失败，密码错误");

}
if (!userService.checkIsAdmin(userDTO)){
    return JsonData.fail("您不是管理员用户，禁止登陆");
}
return JsonData.success(userDTO);
}
@RequestMapping("/userlist")
    @ResponseBody
    public JsonData list(){
    if (JedisUtil.get(Const.USERLIST_PREFIX,"")==null){
       List<UserDTO>userDTOList= (List<UserDTO>) userService.listUser().getData();
JedisUtil.setEx(Const.USERLIST_PREFIX,"",JsonUtil.Obj2String(userDTOList),60);
return JsonData.success(userDTOList);
    }
    return JsonData.success(JsonUtil.String2Obj(JedisUtil.get(Const.USERLIST_PREFIX,""),new TypeReference<List<UserDTO>>(){}));
}
}
